package com.sk.sourcecircle.activity.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.adapter.NoticeAdapter;
import com.sk.sourcecircle.base.BaseActivity;
import com.sk.sourcecircle.callback.HandleDataCallBack;
import com.sk.sourcecircle.entity.NoticeInfoBean;
import com.sk.sourcecircle.utils.AppApi;
import com.sk.sourcecircle.utils.MyUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by sk on 2017/12/25.
 */

public class NoticeActivity extends BaseActivity implements HandleDataCallBack, BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;
    private LoadingDialog loadingDialog;

    private String community_id;
    private int page = 1;

    List<NoticeInfoBean.DataBean> allDataList = new ArrayList<>();
    private NoticeAdapter adapter;
    private boolean isLoadMore = false;

    @Override
    protected int getContentView() {
        return R.layout.activity_notice;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadingDialog = MyUtils.getLoadingDialog(mContext, false);
        community_id = getIntent().getStringExtra("community_id");
        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new NoticeAdapter(R.layout.item_notice, allDataList);
        dataRyc.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
        adapter.setOnLoadMoreListener(this, dataRyc);
        loadingDialog.show();

        if (community_id != null && community_id.length() > 0) {
            if (community_id.equals("-1")) {
                AppApi.getNotice(page, "2", null, community_id, this, 0, this);
            } else {
                AppApi.getNotice(page, "1", null, community_id, this, 0, this);
            }
        } else {
            AppApi.getAppNotice(this, 1, this);
        }


    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        NoticeInfoBean noticeInfoBean = JSON.parseObject(result, NoticeInfoBean.class);
        List<NoticeInfoBean.DataBean> dataBeanList = noticeInfoBean.getData();
        if (page == 1 && allDataList.size() > 0) {
            allDataList.clear();
        }
        allDataList.addAll(dataBeanList);
        if (allDataList.size() < 10) {
            adapter.setEnableLoadMore(false);
        } else {
            adapter.setEnableLoadMore(true);
        }
        if (isLoadMore) {
            adapter.loadMoreComplete();
            if (dataBeanList.size() == 0) {
                adapter.loadMoreEnd();
            }
            isLoadMore = false;
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void handlerFailData(String failString, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void handleErrorData(String error, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(mContext, DetailsOfConsultationActivity.class);
        intent.putExtra("id", allDataList.get(position).getArticle_id());
        startActivity(intent);
    }

    @Override
    public void onLoadMoreRequested() {
        isLoadMore = true;
        page++;
        AppApi.getNotice(page, "1", null, community_id, this, 0, this);
    }
}
